<template>
  <div id="app">
    <img alt="Vue logo" src="./assets/logo.png">
    <HelloWorld msg="Welcome to Your Vue.js App"/>
    
    <!-- 路由导航 -->
    <div class="nav-links">
      <router-link to="/foo" class="link">Go to Foo</router-link> | 
      <router-link to="/bar" class="link">Go to Bar</router-link>
    </div>
    
    <!-- 显示获取的数据 -->
    <div v-if="info">
      <h3>获取的数据：</h3>
      <pre>{{ info }}</pre>
    </div>
    <div v-else>
      <p>加载中...</p>
    </div>
    
    <!-- 路由出口 -->
    <div class="router-view-container">
      <h3>当前路由组件：</h3>
      <router-view></router-view>
    </div>
    
    <!-- JSONP 搜索部分 -->
    <div class="search-container">
      <h3>百度搜索建议（JSONP）</h3>
      请输入关键字：<input type="text" v-model="keyword" @keyup="sendJsonP(keyword)">
      <ul>
        <li v-for="r in result" :key="r">{{ r }}</li>
      </ul>
    </div>
  </div>
</template>

<script>
import HelloWorld from './components/HelloWorld.vue'
import axios from 'axios'

export default {
  name: 'App',
  components: {
    HelloWorld
  },
  data(){
    return {
      info: null,
      keyword: '', // 添加 keyword 数据
      result: []   // 添加 result 数据
    }
  },
 methods: {
  sendJsonP(keyword) {
    if (!keyword) {
      this.result = [];
      return;
    }
    
    // 使用JSONPlaceholder API进行模拟搜索
    axios
      .get(`https://jsonplaceholder.typicode.com/posts`)
      .then(response => {
        console.log('搜索响应：', response);
        // 过滤包含关键词的标题
        const filteredResults = response.data.filter(item => 
          item.title.toLowerCase().includes(keyword.toLowerCase())
        );
        
        if (filteredResults.length > 0) {
          this.result = filteredResults.slice(0, 5).map(item => 
            `标题: ${item.title}`
          );
        } else {
          this.result = [`没有找到包含"${keyword}"的帖子`];
        }
      })
      .catch(error => {
        console.log('搜索错误：', error);
        this.result = ['搜索失败，请检查网络'];
      });
  }
},
  mounted(){
    console.log('App组件已挂载');
    
    // 使用更可靠的API端点
    axios
      .get('https://jsonplaceholder.typicode.com/posts/1')
      .then(response => {
        console.log('API响应：', response);
        this.info = response.data;
      })
      .catch(error => {
        console.log('API错误：', error);
        this.info = { error: '请求失败' };
      });
  }
}
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}

.nav-links {
  margin: 20px 0;
}

.link {
  margin: 0 10px;
  text-decoration: none;
  color: #42b983;
}

.link:hover {
  text-decoration: underline;
}

.router-view-container {
  margin: 20px auto;
  padding: 20px;
  border: 1px solid #ddd;
  max-width: 600px;
}

.search-container {
  margin: 20px auto;
  padding: 20px;
  border: 1px solid #ddd;
  max-width: 600px;
}

pre {
  text-align: left;
  background: #f5f5f5;
  padding: 10px;
  border-radius: 5px;
}

input[type="text"] {
  padding: 8px;
  margin: 10px;
  border: 1px solid #ddd;
  border-radius: 4px;
}

ul {
  list-style-type: none;
  padding: 0;
}

li {
  padding: 5px;
  border-bottom: 1px solid #eee;
}
</style>